package model

import business.*
import enums.Direction
import org.itheima.kotlin.game.core.Composer
import org.itheima.kotlin.game.core.Painter

/**
 * 移动的能力
 * 射击的能力
 * 阻塞移动的能力
 * 接受攻击的能力
 * 销毁的能力
 * */
abstract class Tank(override var x: Int, override var y: Int) : Moveable,Shotable,Blockable,Sufferable,Destoryable {

    override var currentDirection: Direction=Direction.UP
    override val width: Int=GameConfig.BLOCK_WIDTH
    override val height: Int=GameConfig.BLOCK_WIDTH
    override var badDirection: Direction?=null

    override var pool: Int=1

    var drawCount=0 //draw执行次数
    var imgPath:String="" //图片路径

    override fun draw() {
        if(drawCount<4){
            imgPath=when(drawCount){
                0->"img/born_1.gif"
                1->"img/born_2.gif"
                2->"img/born_3.gif"
                3->"img/born_4.gif"
                else -> imgPath
            }
        }
        drawCount++
        Painter.drawImage(imgPath,x,y)
    }

    override fun move(direction: Direction) {
        if(drawCount<4){
            return
        }

        if(this.currentDirection!=direction){ //移动方向与当前方向不同，那么此次移动只改变方向
            this.currentDirection=direction
            return
        }

        if(badDirection==currentDirection){ //将要发生碰撞与当前方向相同
            return
        }

        when(currentDirection){
            Direction.UP->y-=speed
            Direction.DOWN->y+=speed
            Direction.LEFT->x-=speed
            Direction.RIGHT->x+=speed
        }

        /** 越界判断 */
        if(x<0) x=0
        if(GameConfig.WINDOW_WIDTH-this.width<x) x=GameConfig.WINDOW_WIDTH-this.width
        if(y<0) y=0
        if(GameConfig.WINDOW_HEIGHT-this.height<y) y=GameConfig.WINDOW_HEIGHT-this.height
    }

    override fun notifyCollision(block: Blockable?, direction: Direction?) {
        this.badDirection=direction
    }

    /**
     * 生产子弹的方法
     *
     * */
    override fun shot(gradle:Int): Bullet? {
        return Bullet(gradle,this,currentDirection,{bulletWidth,bulletHeight->
            var bulletX=0
            var bulletY=0
            var tankX=this.x
            var tankY=this.y
            var tankWidth=this.width
            var tankHeight=this.height

            when(currentDirection){
                Direction.UP->{
                    bulletX= tankX+(tankWidth-bulletWidth)/2
                    bulletY=tankY-bulletHeight/2
                }
                Direction.DOWN->{
                    bulletX= tankX+(tankWidth-bulletWidth)/2
                    bulletY= tankY+tankHeight-bulletHeight/2
                }
                Direction.LEFT->{
                    bulletX=tankX-bulletWidth/2
                    bulletY=tankY+(tankHeight-bulletHeight)/2
                }
                Direction.RIGHT->{
                    bulletX= tankX+tankWidth-bulletWidth/2
                    bulletY=tankY+(tankHeight-bulletHeight)/2
                }
            }


            Pair(bulletX,bulletY)
        })
    }

    override fun notifySuffer(attack: Attackable): ArrayList<IView> {
        var list=ArrayList<IView>()

        pool-=attack.power
        if(pool<=0){ //被打爆了
            //播放爆炸的声音
            Composer.play("snd/blast.wav")
            list.add(Blast(x,y))
        }else{
            //播放子弹打墙的声音
            Composer.play("snd/hit.wav")
        }
        return list
    }

    override fun isDestory(): Boolean {
        return pool<=0
    }
}